package com.southpark.system.managers
{
	import com.southpark.framework.managers.ErrorManager;
	import com.southpark.system.enum.ScreenModeEnum;
	import com.southpark.system.interfaces.managers.IApplicationManager;
	import com.southpark.system.interfaces.managers.IPopUpManager;
	import com.southpark.system.interfaces.view.IViewController;
	import com.southpark.system.interfaces.view.IViewUI;
	
	import flash.display.DisplayObject;
	import flash.utils.Dictionary;
	
	import mx.core.Container;
	import mx.core.UIComponent;

	
	[Bindable]
	public class PopUpManager implements IPopUpManager
	{
		
		public function PopUpManager(applicationManager:IApplicationManager)
		{
			this.applicationManager = applicationManager;
			createPopUpDictionary();
		}
		
		
		/**
		 * 
		 */
		private var applicationManager:IApplicationManager;
		private var popUpDictionary:Dictionary;
		
		
		/**
		 * screen mode
		 */
		private var _screenMode:ScreenModeEnum;
		public function get screenMode():ScreenModeEnum
		{
			return _screenMode ? _screenMode : ScreenModeEnum.STANDARD;
		};
		public function set screenMode(value:ScreenModeEnum):void
		{
			_screenMode = value;
		};
		
		
		/**
		 * 
		 */
		private function createPopUpDictionary():void
		{
			popUpDictionary = new Dictionary();
			
			for each (var item:Array in applicationManager.popUpMap)
			{
				definePopUp(item[0],item[1]);
			};
		}
		
		private function definePopUp(popUpInterface:Class,popUpClass:Class):void
		{	
		 	if (popUpDictionary[popUpInterface])
		 	{
		 		ErrorManager.throwError(ErrorManager.DUPLICATE_DEFINITION,this);
		 		return;
		 	};
		 
		 	popUpDictionary[popUpInterface] = popUpClass;
		}
		
		private function get modalPopUpLayer():Container
		{
			return applicationManager.background.modalPopUpLayer;
		}
		
		private function get floatingPopUpLayer():Container
		{
			return applicationManager.background.floatingPopUpLayer;
		}
		
		
		/**
		 * 
		 */
		public function addPopUp(popUpInterface:Class,killOtherPopUps:Boolean=true,isModal:Boolean=true,isRegistered:Boolean=true):IViewController
		{
			if (!popUpDictionary[popUpInterface]) 
			{
				if (isRegistered)
				{
					applicationManager.infoManager.showApplicationError("Pop-up " + Class(popUpInterface).toString() + " is not included");
					return null;
				};
				popUpDictionary[popUpInterface] = popUpInterface;
			};
			
			var layer:Container;
			var popUp:UIComponent;
			
			if (isModal)
			{
				layer = modalPopUpLayer;
				screenMode = ScreenModeEnum.POPUP;
			}
			else
			{
				layer = floatingPopUpLayer;
			};
			
			if (killOtherPopUps) 
			{
				layer.removeAllChildren();
			};
			
			if (popUpDictionary[popUpInterface] is IViewUI)
		 	{
		 		popUp = popUpDictionary[popUpInterface] as UIComponent;
		 		
		 		if (layer.contains(popUp))
		 		{
		 			return IViewUI(popUp).viewController;
		 		};
		 		
		 		layer.addChild(popUp);
		 		IViewUI(popUp).viewController.onShow();
		 	}
		 	else if (popUpDictionary[popUpInterface] is Class)
		 	{
		 		popUp = new popUpDictionary[popUpInterface]();
		 		
		 		popUp.percentWidth = 100;
		 		popUp.percentHeight = 100;
		 		popUp.setStyle("horizontalAlign","center");
		 		popUp.setStyle("verticalAlign","middle");
		 		
		 		layer.addChild(popUp);
		 		IViewUI(popUp).viewController.onCreated();
		 		
		 		popUpDictionary[popUpInterface] = popUp;
		 	};
			
			return IViewUI(popUp).viewController;
		}
		
		
		public function removeAllPopUps():void
		{
			var child:IViewUI;
			var displayObject:DisplayObject
			
			for each (displayObject in modalPopUpLayer.getChildren())
			{
				child = modalPopUpLayer.removeChild(displayObject) as IViewUI;
				if (child) child.viewController.onHide();
			};
			
			for each (displayObject in floatingPopUpLayer.getChildren())
			{
				child = modalPopUpLayer.removeChild(displayObject) as IViewUI;
				if (child) child.viewController.onHide();
			};
			
			screenMode = ScreenModeEnum.STANDARD;
		}
		
		
		public function removePopUp(popUp:DisplayObject):void
		{			
			var layer:Container;
			
			if (modalPopUpLayer.contains(popUp))
			{
				layer = modalPopUpLayer;
			}
			else if (floatingPopUpLayer.contains(popUp))
			{
				layer = floatingPopUpLayer;
			}
			else
			{
				return;
			};
			
			
			/**
			 * removes the selected pop-up and all pop-ups above
			 */
			var index:int = layer.getChildIndex(popUp);
			var lastIndex:int = layer.numChildren - 1;
			var child:IViewUI;
			
			for (var i:int=lastIndex; i>=index; i--)
			{
				child = layer.removeChildAt(i) as IViewUI;
				if (child) child.viewController.onHide();	
			};
			
			
			/**
			 * if no pop-ups remaining kill the pop-up mode
			 */
			if (layer == modalPopUpLayer && layer.numChildren == 0)
			{
				screenMode = ScreenModeEnum.STANDARD;
			};
		}
	}
}